package com.ruoyi.patrol.service.impl;

import com.ruoyi.common.constant.PatrolConstans;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.patrol.domain.PtLineStaff;
import com.ruoyi.patrol.domain.PtSiteRecordStaff;
import com.ruoyi.patrol.domain.vo.PtSiteRecordVo;
import com.ruoyi.patrol.mapper.PtSiteRecordStaffMapper;
import com.ruoyi.patrol.service.IPtLineStaffService;
import com.ruoyi.patrol.service.IPtSiteRecordStaffService;
import com.ruoyi.patrol.utils.OssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 点位巡逻记录(行政)Service业务层处理
 *
 * @author mlb
 * @date 2022-11-23
 */
@Service
public class PtSiteRecordStaffServiceImpl implements IPtSiteRecordStaffService {
    @Autowired
    private PtSiteRecordStaffMapper ptSiteRecordStaffMapper;
    @Autowired
    private IPtLineStaffService ptLineStaffService;

    /**
     * 查询点位巡逻记录(行政)
     *
     * @param id 点位巡逻记录(行政)ID
     * @return 点位巡逻记录(行政)
     */
    @Override
    public PtSiteRecordStaff selectPtSiteRecordStaffById(Long id) {
        return ptSiteRecordStaffMapper.selectPtSiteRecordStaffById(id);
    }

    /**
     * 查询点位巡逻记录(行政)列表
     *
     * @param ptSiteRecordStaff 点位巡逻记录(行政)
     * @return 点位巡逻记录(行政)
     */
    @Override
    public List<PtSiteRecordStaff> selectPtSiteRecordStaffList(PtSiteRecordStaff ptSiteRecordStaff) {
        List<PtSiteRecordStaff> list = ptSiteRecordStaffMapper.selectPtSiteRecordStaffList(ptSiteRecordStaff);

        list.forEach(item -> {
            if (!StringUtils.isEmpty(item.getPhotoUrl())) {
                String s = item.getPhotoUrl().split(".com\\/")[1];
                String ossUrl = OssUtils.getOssUrl(s);
                item.setPhotoUrl(ossUrl);
            }
        });
        return list;
    }

    /**
     * 新增点位巡逻记录(行政)
     *
     * @param ptSiteRecordStaff 点位巡逻记录(行政)
     * @return 结果
     */
    @Override
    public int insertPtSiteRecordStaff(PtSiteRecordStaff ptSiteRecordStaff) {
        ptSiteRecordStaff.setCreateTime(DateUtils.getNowDate());
        return ptSiteRecordStaffMapper.insertPtSiteRecordStaff(ptSiteRecordStaff);
    }

    /**
     * 修改点位巡逻记录(行政)
     *
     * @param record 点位巡逻记录(行政)
     * @return 结果
     */
    @Override
    public int updatePtSiteRecordStaff(PtSiteRecordVo record) {
        PtSiteRecordStaff detail = ptSiteRecordStaffMapper.selectPtSiteRecordStaffById(record.getRecordId());
        if (detail == null) {
            return 0;
        }

        String operator = SecurityUtils.getNickName();
        // 更新巡逻点位记录
        detail.setState(record.getState());
        detail.setPhotoUrl(record.getPhotoUrl());
        detail.setLatitude(record.getLatitude());
        detail.setLongitude(record.getLongitude());
        detail.setUpdateTime(DateUtils.getNowDate());
        detail.setUpdateBy(operator);
        detail.setRemark(record.getRemark());
        detail.setHandleInfo(record.getHandleInfo());
        ptSiteRecordStaffMapper.updatePtSiteRecordStaff(detail);

        PtLineStaff lineStaff = ptLineStaffService.selectPtLineStaffById(detail.getScheduleId());
        // 当巡逻点位异常时，更新线路的异常数量
        if (record.getState() == PatrolConstans.PATROL_SITE_STATUES_ABNORMAL) {
            lineStaff.setPatrolResult(lineStaff.getPatrolResult() + 1);
        }

        // 更新线路的状态
        PtSiteRecordStaff query = new PtSiteRecordStaff();
        query.setScheduleId(detail.getScheduleId());

        List<PtSiteRecordStaff> details = ptSiteRecordStaffMapper.selectPtSiteRecordStaffList(query);
        long undoCount = details.stream().filter(e -> e.getState() == PatrolConstans.PATROL_SITE_STATUES_UNDO).count();
        long abnormalCount = details.stream().filter(e -> e.getState() == PatrolConstans.PATROL_SITE_STATUES_ABNORMAL).count();

        if (undoCount == 0) {
            lineStaff.setPatrolStatus(PatrolConstans.PATROL_SCHEDULE_STATUES_DONE);
        } else {
            lineStaff.setPatrolStatus(PatrolConstans.PATROL_SCHEDULE_STATUES_DOING);
        }
        //异常的个数
        lineStaff.setPatrolResult((int) abnormalCount);

        ptLineStaffService.updatePtLineStaff(lineStaff);

        return 1;
    }

    @Override
    public int handleAbnormal(PtSiteRecordVo record) {
        PtSiteRecordStaff detail = ptSiteRecordStaffMapper.selectPtSiteRecordStaffById(record.getRecordId());
        if (detail == null) {
            return 0;
        }

        // 更新巡逻点位记录
        detail.setState(record.getState());
        detail.setHandleInfo(record.getHandleInfo());
        ptSiteRecordStaffMapper.updatePtSiteRecordStaff(detail);

        // 更新异常个数
        PtSiteRecordStaff query = new PtSiteRecordStaff();
        query.setScheduleId(detail.getScheduleId());

        List<PtSiteRecordStaff> details = ptSiteRecordStaffMapper.selectPtSiteRecordStaffList(query);
        long abnormalCount = details.stream().filter(e -> e.getState() == PatrolConstans.PATROL_SITE_STATUES_ABNORMAL).count();

        PtLineStaff lineStaff = ptLineStaffService.selectPtLineStaffById(detail.getScheduleId());
        lineStaff.setPatrolResult((int) abnormalCount);
        ptLineStaffService.updatePtLineStaff(lineStaff);

        return 1;
    }

    /**
     * 批量删除点位巡逻记录(行政)
     *
     * @param ids 需要删除的点位巡逻记录(行政)ID
     * @return 结果
     */
    @Override
    public int deletePtSiteRecordStaffByIds(Long[] ids) {
        return ptSiteRecordStaffMapper.deletePtSiteRecordStaffByIds(ids);
    }

    /**
     * 删除点位巡逻记录(行政)信息
     *
     * @param id 点位巡逻记录(行政)ID
     * @return 结果
     */
    @Override
    public int deletePtSiteRecordStaffById(Long id) {
        return ptSiteRecordStaffMapper.deletePtSiteRecordStaffById(id);
    }

    @Override
    public long selectCount(PtSiteRecordStaff ptSiteRecordStaff) {
        return ptSiteRecordStaffMapper.selectCount(ptSiteRecordStaff);
    }
}
